package com.bayair.doctor.net.exception;

public class ServerException extends RuntimeException {

    private int errCode;
    private String message;


    public ServerException(int errCode) {
        this.errCode = errCode;
        initMsgByCode(errCode);
    }

    public ServerException(int errCode, String msg) {
        this.errCode = errCode;
        this.message = msg;
    }

    public int getErrCode() {
        return errCode;
    }

    @Override
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    private void initMsgByCode(int errCode) {
        if (errCode == ERROR.PARAM_ERROR) {
            this.errCode = 205;
            setMessage("参数错误");
        }
        if (errCode == ERROR.SIGN_ERROR) {
            this.errCode = 206;
            setMessage("签名错误");
        }
        if (errCode == ERROR.USER_ERROR) {
            setMessage("用户不存在");
        }
        if (errCode == ERROR.ILLEGAL_ERROR) {
            setMessage("来源不合法");
        }
        if (errCode == ERROR.SYSTEM_NOTWORK_ERROR) {
            setMessage("系统维护");
        }
        if (errCode == ERROR.PLATFORM_ERROR) {
            setMessage("平台错误");
        }
        if (errCode == ERROR.OTHER_ERROR) {
            setMessage("其他错误");
        }

        if (errCode == CUSTOM_ERROR.NET_ERROR) {
            setMessage("没有网络");
        }
    }


    /**
     * 约定异常
     */
    public static class ERROR {
        /**
         * 参数错误 改成识别205
         */
        public static final int PARAM_ERROR = 200;
        /**
         * 签名错误 改成识别206
         */
        public static final int SIGN_ERROR = 201;
        /**
         * 用户不存在
         */
        public static final int USER_ERROR = 401;
        /**
         * 来源不合法
         */
        public static final int ILLEGAL_ERROR = 402;

        /**
         * 系统维护
         */
        public static final int SYSTEM_NOTWORK_ERROR = 997;

        /**
         * 平台错误
         */
        public static final int PLATFORM_ERROR = 998;

        /**
         * 其他错误
         */
        public static final int OTHER_ERROR = 999;
    }

    /**
     * 自定义异常
     */
    public static class CUSTOM_ERROR {
        /**
         * 无网络
         */
        public static final int NET_ERROR = 301;

    }

}